package com.fanghaoran.notes.service.impl;

import com.fanghaoran.notes.bean.MyResult;
import com.fanghaoran.notes.dao.CartDao;
import com.fanghaoran.notes.dao.impl.CartDaoImpl;
import com.fanghaoran.notes.entity.Cart;
import com.fanghaoran.notes.service.CartService;
import com.fanghaoran.notes.util.Constants;
import com.fanghaoran.notes.util.StringUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/04/22/14:06
 * @Description:
 */
public class CartServiceImpl implements CartService {
    /**
     * 购物车dao
     */
    CartDao cartDao=new CartDaoImpl();
    @Override
    public MyResult hasCart(int uId, int pId) throws Exception {
        //数据库查询
        List<Cart> carts = cartDao.selectCartByUidAndPid(uId, pId);
        return StringUtil.returnOfSelect(carts);
    }

    @Override
    public MyResult addCart(Cart cart) throws Exception {
        Integer rows = cartDao.insertCart(cart);
        return StringUtil.returnOfUpdate(rows);
    }

    @Override
    public MyResult updateCart(Cart cart) throws SQLException {
        Integer rows = cartDao.updateCart(cart);
        return StringUtil.returnOfUpdate(rows);
    }

    @Override
    public MyResult showCart(int uId) throws Exception {
        List<Cart> carts = cartDao.selectCartByUid(uId);
        return StringUtil.returnOfSelect(carts);
    }

    @Override
    public MyResult deleteCartByPid(int pId) throws SQLException {
        Integer rows = cartDao.deleteCartByPid(pId);
        return StringUtil.returnOfUpdate(rows);
    }

    @Override
    public MyResult removeAllCart(int uId) throws SQLException {
        Integer rows = cartDao.deleteCartByUid(uId);
        return StringUtil.returnOfUpdate(rows);
    }


}
